package accounting.action;

import accounting.model.Asset;
import accounting.service.AssetService;
import com.opensymphony.xwork2.ActionSupport;
import manage.model.AssetType;
import manage.service.AssetTypeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuweigui
 */
@SuppressWarnings("serial")
public class AssetAction extends ActionSupport {

    private List<Asset> assets = new ArrayList<Asset>();

    private String assetTypesJSON;

    private AssetService assetService;
    private AssetTypeService assetTypeService;
    private Asset asset = new Asset();
    //for ajax call 
    //result="success" if successful, otherwise error message content
    private String result;

    public String delete() {
        assetService.remove(asset.getId());
        return SUCCESS;
    }

    public String execute() {
        assets = assetService.findAll();

        List<AssetType> assetTypes = assetTypeService.findAll();
        StringBuilder sb = new StringBuilder();
        sb.append("assetTypes:[");
        for (AssetType assetType : assetTypes) {
            sb.append(assetType.toJSON()).append(",");
        }
        sb.append("]");

        return SUCCESS;
    }

    public String add() {
        assetService.add(asset);
        return SUCCESS;
    }

    public List<Asset> getAssets() {
        return assets;
    }

    public void setAssets(List<Asset> assets) {
        this.assets = assets;
    }

    public String getAssetTypesJSON() {
        return assetTypesJSON;
    }

    public void setAssetTypesJSON(String assetTypesJSON) {
        this.assetTypesJSON = assetTypesJSON;
    }

    public AssetService getAssetService() {
        return assetService;
    }

    public void setAssetService(AssetService assetService) {
        this.assetService = assetService;
    }

    public AssetTypeService getAssetTypeService() {
        return assetTypeService;
    }

    public void setAssetTypeService(AssetTypeService assetTypeService) {
        this.assetTypeService = assetTypeService;
    }

    public Asset getAsset() {
        return asset;
    }

    public void setAsset(Asset asset) {
        this.asset = asset;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }


}
